//
//  YPMainController.m
//  project
//
//  Created by Jtg_yao on 2018/10/12.
//  Copyright © 2018年 rocHome. All rights reserved.
//

#import "YPMainController.h"
#import "YPBaseTableViewCell.h"
#import "YPBaseTableView.h"
#import <UShareUI/UShareUI.h>

@interface YPMainController ()

@end

@implementation YPMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"你大爷的测试版";
    self.statisticsPage = YES;
    
    [self setNavigationItem];
    [self setTableViewAttribute];
    [self requestDataSource];
}

- (void)setNavigationItem {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(rightAction)];
}

- (void)setTableViewAttribute {
    
    [self.tableView registerClass:[YPBaseTableViewCell class] forCellReuseIdentifier:NSStringFromClass([YPBaseTableViewCell class])];
}

-(void)requestDataSource {
    
    [YPProgressHUD showDIYLoading];
    
    YPBaseModel *model = [YPBaseModel new];
    
    [self.dataList addObjectsFromArray:@[model,model,model,model]];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [YPProgressHUD dismissHUD];
        [self.tableView pullRefreshEnd];
        self.tableView.emptyStatus = YPPageEmptyStatusHidden;
        [self.tableView reloadData];
    });
}

#pragma mark -- TableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YPBaseTableViewCell *cell = [YPCellFactory createCellWithModel:[YPBaseModel new] tableView:tableView indexPath:indexPath];
    
    UILabel *wcLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, self.view.bounds.size.width - 30, 34.0f)];
    [self.view addSubview:wcLabel];
    wcLabel.backgroundColor = [UIColor darkGrayColor];
    wcLabel.textColor = [UIColor whiteColor];
    wcLabel.font = [UIFont systemFontOfSize:16];
    wcLabel.numberOfLines = 2;
    wcLabel.text = @"welcome to common project !";
    wcLabel.textAlignment = NSTextAlignmentCenter;
    
    [cell.contentView addSubview:wcLabel];
    
    return cell;
}

- (void)rightAction {
    
    [self.navigationController pushViewController:[YPMainController new] animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [self shareImageAndTextToPlatformType:platformType];
    }];
}

- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //设置文本
    messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用，快速武装App。";
    
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = [UIImage imageNamed:@"refresh_1"];
    [shareObject setShareImage:[UIImage imageNamed:@"refresh_1"]];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
